/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.helpclasses;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author juro
 */
public class PlayerManager {
    
    private static List<PlayerDTO> list;
    private static long id = 3;
    
    
    public PlayerManager() {
        if(list != null)return;
        list = new ArrayList<PlayerDTO>();
        PlayerDTO player = new PlayerDTO();
        player.setFirstName("Firstname");
        player.setLastName("Lastname");
        player.setId(1L);
        player.setBirthDate(new Date());
        player.setHeight(180);
        player.setWeight(100);
        list.add(player);
        player = new PlayerDTO();
        player.setFirstName("hrac");
        player.setLastName("hracovic");
        player.setId(2L);
        player.setBirthDate(new Date(1000, 10, 11));
        player.setHeight(170);
        player.setWeight(90);
        list.add(player);
    }
    

    public List<PlayerDTO> findAll() {
        return list;
    }

    public PlayerDTO find(int parseInt) {
        for(PlayerDTO player : list){
            if(player.getId() == parseInt){
                return player;
            }
        }
        return null;
    }

    public void create(PlayerDTO player) {
        player.setId(id);
        list.add(player);
        id++;
    }

    public void edit(PlayerDTO playerDto) {
        for(PlayerDTO player : list){
            if(player.getId() == playerDto.getId()){
                player.setBirthDate(playerDto.getBirthDate());
                player.setFirstName(playerDto.getFirstName());
                player.setHeight(playerDto.getHeight());
                player.setWeight(playerDto.getWeight());
                player.setLastName(playerDto.getLastName());
                return;
            }
        }
    }

    public void delete(PlayerDTO playerDto) {
        int index = -1;
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).getId() == playerDto.getId()){
                index = i;
            }
        }
        if(index >= 0){
            list.remove(index);
        }
    }
    
}
